package com.xingkeduo.service.impl;

import com.xingkeduo.dao.workpad.TStaffClockInMapper;
import com.xingkeduo.model.dto.ShopIdAndStaffInfoDto;
import com.xingkeduo.model.dto.StaffTimingTaskDto;
import com.xingkeduo.model.po.TStaffAttendance;
import com.xingkeduo.model.po.TStaffClockIn;
import com.xingkeduo.model.po.TStoreStaff;
import com.xingkeduo.model.vo.ShopVo;
import com.xingkeduo.service.OtherService;
import com.xingkeduo.service.StaffAttendanceService;
import com.xingkeduo.service.StoreStaffService;
import com.xingkeduo.service.TimingTaskService;
import com.xingkeduo.util.TimeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Description TODO
 * @Date 2019/5/24 14:43
 * @Created 情绪十分不稳定的Shadow
 */

@Service
public class TimingTaskServiceImpl implements TimingTaskService {

    private static Logger log = LoggerFactory.getLogger(TimingTaskServiceImpl.class);

    @Autowired
    private StaffAttendanceService staffAttendanceService;

    @Autowired
    private TStaffClockInMapper staffClockInMapper;

    @Autowired
    private OtherService otherService;

    @Autowired
    private StoreStaffService storeStaffService;

    @Override
    public int clockTask() {
        List<Integer> ids = new ArrayList<>();
        //1.查询当天所有打过上班卡，未达下班卡的考勤ID集合 TODO 测试数据正式把时间改为当天时间
        String selectDate = TimeUtil.DateToFormat(new Date(), "yyyy-MM-dd");
        List<TStaffAttendance> staffAttendances = staffAttendanceService.getAttendanceIdByStatusAndDate(1, selectDate);
        for(TStaffAttendance staffAttendance : staffAttendances){
            System.out.println(staffAttendance.getId());
            ids.add(staffAttendance.getId());
        }
        if(CollectionUtils.isEmpty(staffAttendances)){
            return 0;
        }
        //2.批量跑批执行打卡逻辑
        //2-1.批量打卡 考勤表
        int attendanceResult = staffAttendanceService.batchUpdateStatusById(ids);
        System.out.println(attendanceResult);
        //2-2.批量打卡 打卡历史表
        List<TStaffClockIn> staffClockIns = conver(staffAttendances);
        int clockInResult = staffClockInMapper.batchInsertClockIn(staffClockIns);
        if( attendanceResult > 0 && clockInResult > 0){
            return 1;
        }
        return 0;
    }

    @Override
    public int attendanceTask() {
        //1.根据店铺营业状态获取店铺Id
        List<ShopVo> shops = otherService.getShopByStatus(2L);
        List<Long> shopIds = new ArrayList<>();
        for(ShopVo shopVo : shops){
            shopIds.add(shopVo.getId());
        }
        //2.根据店铺List<id> 获取店铺下签约发型师信息
        //3.把返回发型师数据 跟 店铺name拼接
        // TODO 假数据 2019-05-29  后面需要替换成批量绑定人员
        List<ShopIdAndStaffInfoDto> shopIdAndStaffInfoDtos = otherService.getStaffTimingTaskDto(shopIds);
        for(ShopVo shopVo : shops){
            for(ShopIdAndStaffInfoDto shopIdAndStaffInfoDto : shopIdAndStaffInfoDtos){
                if(shopVo.getId().equals(shopIdAndStaffInfoDto.getId())){
                    shopIdAndStaffInfoDto.setName(shopVo.getName());
                }
            }
        }
        // TODO 之后要修改成 批量新增人员
        for(ShopIdAndStaffInfoDto shopIdAndStaffInfoDto : shopIdAndStaffInfoDtos){
            List<StaffTimingTaskDto> craftsmanList = shopIdAndStaffInfoDto.getCraftsmanList();
            for(StaffTimingTaskDto timingTaskDto : craftsmanList){
                TStoreStaff storeStaff = storeStaffService.getStaffToStoreForWeek(getFetureDate(7), Long.parseLong(timingTaskDto.getId()), shopIdAndStaffInfoDto.getId());
                if(null != storeStaff){
                    continue;
                }
                log.info("入参：");
                log.info("发型师ID:"+timingTaskDto.getId());
                log.info("发型师Name:"+timingTaskDto.getName());
                log.info("店铺ID:"+shopIdAndStaffInfoDto.getId());
                log.info("店铺Name:"+shopIdAndStaffInfoDto.getName());
                storeStaffService.bingStaffToStoreForWeek(
                        Long.parseLong(timingTaskDto.getId()),
                        timingTaskDto.getName(),
                        shopIdAndStaffInfoDto.getId(),
                        shopIdAndStaffInfoDto.getName(),
                        null,
                        getFetureDate(7));
            }
        }
        //3.批量生成排班
        return 1;
    }

    private List<TStaffClockIn> conver(List<TStaffAttendance> staffAttendances){
        List<TStaffClockIn> clockIns = new ArrayList<>();
        for(TStaffAttendance attendance : staffAttendances){
            TStaffClockIn staffClockIn = new TStaffClockIn();
            staffClockIn.setStaffId(attendance.getStaffId());
            staffClockIn.setStaffName(attendance.getStaffName());
            staffClockIn.setStoreId(attendance.getStoreId());
            staffClockIn.setStoreName(attendance.getStoreName());
            staffClockIn.setDate(new Date());
            staffClockIn.setRealWorkStopTime(TimeUtil.stringToFormatDate("1970-01-01 23:59:59"));
            staffClockIn.setEnable(true);
            clockIns.add(staffClockIn);
        }
        return clockIns;
    }

    public static Date getFetureDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + past);
        Date today = calendar.getTime();
        return today;
    }
}
